package be.nvlooy.moviereminder.view;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView;

@Component("coverView")
public class CoverView extends AbstractView {

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, 
    		HttpServletResponse response) throws Exception {
        ServletOutputStream out = null;
        
        setContentType("image/jpg");
        
        byte[] cover = (byte[]) model.get("cover");
        
        if (cover == null || cover.length == 0)
        	return;
        
        try {
            out = response.getOutputStream();
            response.setContentType(getContentType());
            response.setContentLength(cover.length);
            out.write(cover);
            out.flush();
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        finally {
            if (out != null) 
            	out = null;
        }
    }
}
